package com.learn.Callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


public class CallableTest {

    public static void main(String[] args) {
        try {
            MyThread my = new MyThread();
            FutureTask task = new FutureTask(my);
            new Thread(task,"A").start();
            new Thread(task,"B").start();
            // task.get() 为阻塞方法,使用时需要注意放在最后使用
            // 多个方法调用时只调用一次"call" 存在缓存 提高效率
            Integer o = (Integer)task.get();
            System.out.println(o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class MyThread implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        System.out.println("call");
        return 1024;
    }
}